﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using D2DEngine;
using D2DEngine.AI;

namespace D2DEngine.Components.AI
{
    public class D2DAIComponent : D2DComponent
    {
        protected D2DSceneObject sceneObject;
        protected D2DBehavior behavior = null;

        /// <summary>
        /// The current behavior of the scene object
        /// </summary>
        public D2DBehavior Behavior
        {
            get { return this.behavior; }
            set { this.behavior = value; }
        }

        public D2DAIComponent()
        {
        }

        public override void OnRegister()
        {
            this.sceneObject = this.owner as D2DSceneObject;

            base.OnRegister();
        }

        public override void Update(GameTime gameTime)
        {
            // Choose a behavior
            ChooseBehavior();

            // Update chosen behavior
            if(this.behavior != null)
                this.behavior.Update(gameTime);

            base.Update(gameTime);
        }

        /// <summary>
        /// Chooses the behavior
        /// </summary>
        public virtual void ChooseBehavior()
        {
        }

        /// <summary>
        /// Sets the current behavior of the artificial intelligence
        /// </summary>
        /// <param name="behavior">The behavior</param>
        public void SetBehavior(D2DBehavior behavior)
        {
            behavior.SceneObject = this.sceneObject;
            behavior.OnRegister();
            
            this.behavior = behavior;
        }

        protected override void CopyTo(D2DComponent component)
        {
            base.CopyTo(component);

            D2DAIComponent comp = (D2DAIComponent)component;
            comp.Behavior = this.behavior;
        }
    }
}
